package com.bo.day20240924;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

/**
 * 找到二叉树最大宽度
 * 思路：
 * 1. 宽度优先遍历，记录每层节点的个数，并记录最大值
 *
 * @Author: gpb
 * @Date: 2024/9/24 16:07
 * @Description:
 */
public class TreeMaxWidth {
    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node (int v) {
            value = v;
        }
    }

    public static int maxWidth (Node head) {
        if (head == null) {
            return 0;
        }
        // 宽度优先遍历队列
        Queue<Node> queue = new LinkedList<>();
        queue.offer(head);
        // 最大宽度
        int maxWidth = 0;
        // 当前层结束节点
        Node curEnd = head;
        // 下一层的结束节点
        Node nextEnd = null;
        // 当前节层的节点个数
        int curLevelWidth = 0;
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            // 队列添加顺序，左，右
            if (cur.left != null) {
                queue.add(cur.left);
                // 下一层的结束节点
                nextEnd = cur.left;
            }
            if (cur.right != null) {
                queue.add(cur.right);
                nextEnd = cur.right;
            }
            // 统计当前层节点数量
            curLevelWidth++;
            // 当前节点是当前层结束节点，更新最大宽度，并更新当前层结束节点
            if (cur == curEnd) {
                // 最宽的节点数量
                maxWidth = Math.max(maxWidth, curLevelWidth);
                // 清空当前节点数量
                curLevelWidth = 0;
                // 更新下一层的结束节点
                curEnd = nextEnd;
            }
        }
        return maxWidth;
    }

    // for test
    public static Node generateRandomBST (int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    // for test
    public static Node generate (int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static void main (String[] args) {
        int maxLevel = 10;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            if (maxWidthUseMap(head) != maxWidth(head)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");

    }

    public static int maxWidthUseMap (Node head) {
        if (head == null) {
            return 0;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        // key 在 哪一层，value
        HashMap<Node, Integer> levelMap = new HashMap<>();
        levelMap.put(head, 1);
        int curLevel = 1; // 当前你正在统计哪一层的宽度
        int curLevelNodes = 0; // 当前层curLevel层，宽度目前是多少
        int max = 0;
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            int curNodeLevel = levelMap.get(cur);
            if (cur.left != null) {
                levelMap.put(cur.left, curNodeLevel + 1);
                queue.add(cur.left);
            }
            if (cur.right != null) {
                levelMap.put(cur.right, curNodeLevel + 1);
                queue.add(cur.right);
            }
            if (curNodeLevel == curLevel) {
                curLevelNodes++;
            } else {
                max = Math.max(max, curLevelNodes);
                curLevel++;
                curLevelNodes = 1;
            }
        }
        max = Math.max(max, curLevelNodes);
        return max;
    }

}
